package com.suredy.app.porject.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.porject.model.BidAgency;
import com.suredy.app.porject.service.BidAgencySrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;
import com.suredy.security.service.OrgSrv;

/**
 * 招标代理机构控制器
 * 
 * @author VIVID.G
 * @since 2015-10-12
 * @version v0.1
 */
@Controller
@RequestMapping("/config/bid-agency")
public class BidAgencyCtrl extends BaseCtrl {

	@Autowired
	private BidAgencySrv bidAgencySrv;

	@Autowired
	private OrgSrv orgSrv;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Boolean admin, String id, Model model) {
		String view = "app/agency/bid-agency-create";

		if (Boolean.TRUE.equals(admin)) {
			model.addAttribute("admin", true);
			model.addAttribute("orgs", this.orgSrv.getAll());
		} else {
			model.addAttribute("admin", false);
		}

		if (!StringUtils.isBlank(id)) {
			String ql = "SELECT T, T.owner.id FROM BidAgency T WHERE T.id = ?";

			Object[] data = (Object[]) this.bidAgencySrv.readSingleByQL(ql, id);

			if (data != null && data.length == 2) {
				model.addAttribute("data", data[0]);
				model.addAttribute("ownerId", data[1]);
			}
		}

		return view;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Integer page, Integer pageSize, Model model) {
		String view = "app/agency/list";

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		String ql = "SELECT T, T.owner FROM BidAgency T ORDER BY T.owner, T.company";

		@SuppressWarnings("unchecked")
		List<Object[]> data = (List<Object[]>) this.bidAgencySrv.readPageByQL(ql, page, pageSize);

		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bidAgencySrv.getCountByEntity(null));

		return view;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Object create(BidAgency data) {
		if (data == null || StringUtils.isBlank(data.getCompany()) || data.getOwner() == null || StringUtils.isBlank(data.getOwner().getId()))
			return MessageModel.createErrorMessage("提交的数据无效！", null);

		String sql;
		List<Object> params = new ArrayList<Object>();

		if (StringUtils.isBlank(data.getId())) {
			sql = "select exists(select id from tb_bid_agency where company = ? and org_id = ?)";

			params.add(data.getCompany());
			params.add(data.getOwner().getId());
		} else {
			sql = "select exists(select id from tb_bid_agency where company = ? and org_id = ? and id <> ?)";

			params.add(data.getCompany());
			params.add(data.getOwner().getId());
			params.add(data.getId());
		}

		int exists = this.bidAgencySrv.getCountBySQL(sql, params.toArray());

		if (exists == 1)
			return MessageModel.createErrorMessage("exists", null);

		if (!StringUtils.isBlank(data.getUrl())) {
			if ("http://".equalsIgnoreCase(data.getUrl()))
				data.setUrl(null);
			else
				data.setUrl(data.getUrl().trim());
		}

		BidAgency b = this.bidAgencySrv.saveOrUpdate(data);

		if (b == null || StringUtils.isBlank(b.getId()))
			return MessageModel.createErrorMessage("数据保存失败！", null);

		return MessageModel.createSuccessMessage("操作成功！", b);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam("ids[]") String[] ids) {
		if (ids == null || ids.length <= 0)
			return MessageModel.createSuccessMessage("操作成功！", null);

		this.bidAgencySrv.deleteByIds(ids);

		return MessageModel.createSuccessMessage("操作成功！", null);
	}

}
